package manegement;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import dao.DBFacade;

import bean.UserBean;

public class UserManager {
	
	private HashMap<String, UserBean> users;
	
	private HashMap<String, BlogManager> blogsByUser;
	
	private HashMap<String, AnnoucementManager> annoucementsByUser; 
	
	
	public UserManager(){
		users = new HashMap<String, UserBean>();
		blogsByUser = new HashMap<String, BlogManager>();
		annoucementsByUser = new HashMap<String, AnnoucementManager>();
		
		
		UserBean user;
		for(Iterator<UserBean> it = DBFacade.getInstance().getUsers().iterator(); it.hasNext();){
			user = it.next();
			users.put(user.getId(), user);
			blogsByUser.put(user.getId(), new BlogManager(user.getId()));
			//TODO annoucementsByUser.put(user.getId(), new AnnoucementManager(user.getId()));
			
		}
	}
	
	//User Operations
	
	public void createUser(String login, String password, String name,
			String email, String gender, String dateOfBirth, String address,
			String interests, String whoIAm, String movies, String musics,
			String books) throws Exception {
		
		if(login == null || login.trim().equals(""))
			throw new Exception("Invalid login.");
		if(!getUserIdByLogin(login).equals(""))
			throw new Exception("Login existing.");
		if (password == null || password.trim().equals(""))
			throw new Exception("Invalid password.");
		if (email == null || email.trim().equals(""))
			throw new Exception("Invalid email.");
		if(existEmail(email))
			throw new Exception("Email existing.");
		if (dateOfBirth == null || parseStringToDate(dateOfBirth) == null)
			throw new Exception("Invalid date of birth.");
		
		String userId = getNewUserId();
		UserBean newUser = new UserBean(login, password, name, email, gender, dateOfBirth, address, interests, whoIAm, movies, musics, books, userId);
		users.put(userId, newUser);
		blogsByUser.put(userId, new BlogManager(userId));
//		annoucementsbyUser.put(userId, new AnnoucementManager()); //TODO
	
	}



	/**
	 * Changes a profile on system based on a given SessionID
	 * 
	 * @param sessionID
	 * @param attribute
	 * @param newValue
	 * @throws Exception
	 */
	public void changeProfileInformation(String userId, String attribute,
			String newValue) throws Exception {
		if(!users.containsKey(userId))
			throw new Exception("Invalid user id.");

		if("Login".equals(attribute) && (newValue == null || newValue.trim().equals("")))
			throw new Exception("Invalid login.");
		if("Login".equals(attribute) && !getUserIdByLogin(newValue).equals(""))
			throw new Exception("Login existing.");
		if ("Password".equals(attribute) && (newValue == null || newValue.equals("") ))
			throw new Exception("Invalid password.");
		if ("Email".equals(attribute) && (newValue == null || newValue.equals("")))
			throw new Exception("Invalid email.");
		if("Email".equals(attribute) && existEmail(newValue))
			throw new Exception("Email existing.");
		if ("Gender".equals(attribute))
			if (newValue == null || !(newValue.equals("Feminino") || newValue.equals("Masculino") || newValue.equals("Não informado")))
				throw new Exception("Sexo inválido");
		if("DateOfBirth".equals(attribute) && parseStringToDate(newValue) != null)
			if(parseStringToDate(newValue) != null)
				throw new Exception("Data inválida");

	
		if(!("Name".equals(attribute) || "Address".equals(attribute) || "Interests".equals(attribute) || 
				"WhoIAm".equals(attribute) || "Movies".equals(attribute) || "Books".equals(attribute) || 
				"Musics".equals(attribute) || "DateOfBirth".equals(attribute)))
			throw new Exception("Attribute inválido");
			
		Object[] args = {newValue};	
		try {
			Class userBeanClass = users.get(userId).getClass();
			Method thisMethod = userBeanClass.getMethod("set" + attribute, new Class[] {String.class});
			thisMethod.invoke(users.get(userId), args);
		} catch (Exception e) {
			throw new Exception("Atributo Inválido");
		}
		
	}
	
	/**
	 * Retrieves a users attribute value
	 * 
	 * @param login
	 *            - User's login
	 * @param attribute
	 *            - Attribute which you wants to know value
	 * @throws Exception
	 *             - Throwns exceptions if given att is invalid
	 */
	
	public String getInformationProfile(String userId, String attribute)
	throws Exception {
		if(!users.containsKey(userId))
			throw new Exception("Invalid user id.");
		try {
			Class userBeanClass = users.get(userId).getClass();
			Method thisMethod = userBeanClass.getMethod("get" + attribute, new Class[] {String.class});
			Object result = thisMethod.invoke(users.get(userId));
			return result.toString();
		} catch (Exception e) {
			throw new Exception("Atributo Inválido");
		}
		

		
		
	}

	/**
	 * Delete a profile
	 * @param sessionId
	 * @throws Exception
	 */
	public void deleteProfile(String userId) throws Exception {
		users.remove(userId);
		blogsByUser.remove(userId);
	}
	
	
	public String getUserIdByLogin(String login){
		UserBean user;
		for(Iterator<UserBean> it = users.values().iterator(); it.hasNext();){
			user = it.next();
			if(user.getLogin().equals(login)){
				return user.getId();
			}
		}
		return null;
	}



	
	//Blog Operations 
	/**
	 * Add a blog
	 * @param title
	 * @param description
	 * @return blogId
	 * @throws Exception
	 */
	public String createBlog(String owner, String title, String description) throws Exception {
		if(blogsByUser.containsKey(owner)){
			return blogsByUser.get(owner).createBlog(owner, title, description);
		}
		return "";
		
	}
	
	/**
	 * Retrieves a post attribute value
	 * 
	 * @param postID
	 * @param attribute
	 *            - Attribute which you wants to know value
	 * @return attribute value
	 * @throws Exception
	 *             - Throwns exceptions if given att is invalid
	 */

	public String getBlogInformation(String userId, String blogId, String attribute)
		throws Exception {
		
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getBlogInformation(blogId, attribute);
		}
		throw new Exception("Blog invalido");
	} 


	
	/**
	 * Modifies the information of the blog passed as parameter
	 * @param attribute
	 * 			- Attribute will be modifies
	 * @param newValue
	 * 			- NewValue will be inserted
	 * @return true 
	 * 			- if the update was completed successfully
	 * 		   false
	 * 			- otherwise 
	 * @throws Exception
	 */
	public void changeBlogInformation(String userId, String blogId, String attribute, String newValue)
	throws Exception {
		if(blogsByUser.containsKey(userId))
			blogsByUser.get(userId).changeBlogInformation(blogId, attribute, newValue);
	}
	
	public void deleteBlog(String userId, String blogId) {
		blogsByUser.get(userId).deleteBlog(blogId);
		
	}
	
	public int getNumberOfBlogs(String userId){
		if(blogsByUser.containsKey(userId)){
			blogsByUser.get(userId).getNumberOfBlogs(userId);
		}
		return 0;
	}
	
	/**
	 * Returns the id of blog at index
	 * @param index
	 * @return subBlogId
	 * @throws Exception
	 */
	public String getBlog(String userId, int index) throws Exception{
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getBlog(userId, index);
		}
		return "";
	}
	
	//SubBlog Operations
	

	public String createSubBlog(String owner, String blogId,  String title, String description) throws Exception {
		if(blogsByUser.containsKey(owner)){
			return blogsByUser.get(owner).createBlog(blogId, title, description);
		}
		return "";
		
	}
	/**
	 * Returns the number of sub blogs
	 * @param blogId
	 * @return number of sub blogs
	 * @throws Exception
	 */
	public int getNumberOfSubBlogs(String userId, String blogId) throws Exception{
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getNumberOfSubBlogs(blogId);
		}
		return 0;		
	}

	/**
	 * Returns the number of all sub blogs
	 * @param blogId
	 * @return number of all sub blogs
	 * @throws Exception
	 */
	public int getNumberOfAllSubBlogs(String userId, String blogId) throws Exception{
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getNumberOfAllSubBlogs(blogId);
		}
		return 0;		
	}
	

	/**
	 * Returns the id of sub blog of blog passed as parameter
	 * @param blogId
	 * @param index
	 * @return subBlogId
	 * @throws Exception
	 */
	public String getSubBlog(String userId, String blogId, int index) throws Exception{
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getSubBlog(blogId, index);
		}
		return "";
	}

	/**
	 * Returns the number of all posts of blog passed as parameter
	 * @param blogId
	 * @return number of all posts
	 * @throws Exception
	 */
	public int getNumberOfAllPosts(String userId, String blogId) throws Exception {
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getNumberOfAllPosts(blogId);
		}
		return 0;
	}
	
	//Annoucement Operations
	
	/**
	 * Returns a number of announcements
	 * @return number of announcements
	 */
	public int getNumberOfAnnouncements() {
		return 0; //TODO announcementList.size();
	}

	/**
	 * Add a post announcement
	 * @param blogId
	 */
	public void addPostAnnouncements(String blogId) {
		//TODO announcementList.add(new Announcement( lastAnnouncementId(), blogId));
		
	}

	/**
	 * Delete a announcement
	 * @param announcementId
	 */
	public void deleteAnnouncement(String announcementId) {
//TODO		Announcement a, a2;
//		a2 = null;
//		for (Iterator<Announcement> it = announcementList.iterator(); it.hasNext();) {
//			a = it.next();
//			if(announcementId.equals(a.getId()));
//				a2 = a;
//		}
//
//		if(a2!= null){
//			announcementList.remove(a2);
//			a2.delete();
//		}


		
	}

	/**
	 * Returns a announcement of index passed as parameter
	 * @param index
	 * @return id of announcement 
	 */
	public String getAnnouncement(int index) {
		return null; //announcementList.get(index).getId();
	}
	
	/**
	 * Returns a announcement blogId
	 * @param announcementId
	 * @return 
	 */
	public String getAnnouncementBlogId(String announcementId) {
//TODO		Announcement a;
//		for (Iterator<Announcement> it = announcementList.iterator(); it.hasNext();) {
//			a = it.next();
//			if(a.getId().equals(announcementId))
//				return a.getBlogId();
//		}
		return null;
	}
	
	
	//Post Operations
	/**
	 * Creare a new post
	 * @param sessionId
	 * @param blogId
	 * @param title
	 * @param text
	 * @return postId
	 * @throws Exception
	 */
	public String createPost(String userId, String blogId, String title, String text, String date) throws Exception {
		if(!blogsByUser.containsKey(userId))
			throw new Exception("Blog Invalido");
		return blogsByUser.get(userId).createPost(blogId, title, text, date);
	}


	/**
	 * Retrieves a post attribute value
	 * 
	 * @param postID
	 * @param attribute
	 *            - Attribute which you wants to know value
	 * @return attribute value
	 * @throws Exception
	 *             - Throwns exceptions if given att is invalid
	 */
	public String getPostInformation(String userId, String blogId, String postId, String attribute) throws Exception{
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getPostInformation(blogId, postId, attribute);
		}
		throw new Exception("Post Invalido");
	} 
	
	/**
	 * Capturing the post with the index passed as parameter
	 * @param blogId
	 * 			- Identifier of the blog which will capture the post
	 * @param index
	 * 			- Post's index
	 * @return postId
	 */

	
	
	public String getPost(String userId, String blogId, int index) {
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getPost(blogId, index);
		}
		return "";
	
	}

	
	/**
	 * Modifies the information of the post passed as parameter
	 * @param sessionId
	 * 			- Identifier of the session to validate the User logon
	 * @param postId
	 * 			- Identifier of the post modification
	 * @param attribute
	 * 			- Attribute to be modified
	 * @param value
	 * 			- Value that will be inserted
	 * @throws Exception
	 */
	public void changePostInformation(String userId, String blogId, String postId,
			String attribute, String value) throws Exception {
		if(!blogsByUser.containsKey(userId))
			throw new Exception("Post Invalido");
		blogsByUser.get(userId).changePostInformation(blogId, postId, attribute, value);
	}

	/**
	 * Delete a post
	 * @param sessionId
	 * 			- Identifier of the session to validate the User logon
	 * @param postId
	 * 			- Identifier of the post will be deleted 
	 * @throws Exception
	 */
	public void deletePost(String userId, String blogId, String postId) {
		if(blogsByUser.containsKey(userId))
			blogsByUser.get(userId).deletePost(blogId, postId);
	}
	
	/**
	 * Returns the number of blog posts
	 * @param blogId
	 * @return number of posts
	 * @throws Exception
	 */
	public int getNumberOfPosts(String userId, String blogId) throws Exception{
		if(blogsByUser.containsKey(userId)){
			return blogsByUser.get(userId).getNumberOfPosts(blogId);
		}
		return 0;
	}


	//Media operations
	/**
	 * Attaches a sound
	 * @param sessionID
	 * 			- Identifier of the session to validate the User logon
	 * @param postID
	 * 			- Identifier of the post that will be inserted audio
	 * @param description
	 * 			- Sound's description
	 * @param data
	 * 			- Data's sound
	 * @return soundId
	 *          - Identifier of the sound
	 * @throws Exception
	 */
	public String attachSound(String userId, String blogId, String postId,
			String description, String data) throws Exception {

		if(!blogsByUser.containsKey(userId))
			throw new Exception("Post Invalido");
		
		return blogsByUser.get(userId).attachSound(blogId, postId, description, data);
		
	}

	/**
	 * Attaches a movie
	 * @param sessionID
	 * 			- Identifier of the session to validate the User logon
	 * @param postID
	 * 			- Identifier of the post that will be inserted sound
	 * @param description
	 * 			- Movie's description
	 * @param data
	 * 			- Data's movie
	 * @return moviedId
	 *          - Identifier of the movie
	 * @throws Exception
	 */
	public String attachMovie(String userId, String blogId, String postID,
			String description, String data) throws Exception {
		if(!blogsByUser.containsKey(userId))
			throw new Exception("Post Invalido");

		return blogsByUser.get(userId).attachMovie(blogId, postID, description, data);
	}


	/**
	 * Attaches a picture
	 * @param sessionID
	 * 			- Identifier of the session to validate the User logon
	 * @param postID
	 * 			- Identifier of the post that will be inserted picture
	 * @param description
	 * 			- Picture's description
	 * @param data
	 * 			- Data's picture
	 * @return pictureId
	 *          - Identifier of the picture
	 * @throws Exception
	 */
	public String attachPicture(String userId, String blogId, String postID,
			String description, String data) throws Exception {
		if(!blogsByUser.containsKey(userId))
			throw new Exception("Post Invalido");

		return blogsByUser.get(userId).attachPicture(blogId, postID, description, data);
	}
	
	/**
	 * Delete a movie
	 * @param sessionId
	 * 			- Identifier of the session to validate the User logon
	 * @param movieId
	 * 			- Identifier of the movie will be deleted
	 */
	public void deleteMovie(String userId, String blogId, String postId, String movieId) {
		if(blogsByUser.containsKey(userId))
			blogsByUser.get(userId).deleteMovie(blogId, postId, movieId); 
	}
	
	/**
	 * Delete a sound
	 * @param sessionId
	 * 			- Identifier of the session to validate the User logon
	 * @param soundId
	 * 			- Identifier of the sound will be deleted
	 */
	public void deleteSound(String userId, String blogId, String postId, String soundId) {
		if(blogsByUser.containsKey(userId))
			blogsByUser.get(userId).deleteSound(blogId, postId, soundId);
		
	}

	/**
	 * Delete a picture
	 * @param sessionId
	 * 			- Identifier of the session to validate the User logon
	 * @param pictureId
	 * 			- Identifier of the picture will be deleted
	 */
	public void deletePicture(String userId, String blogId, String postId, String pictureId) {
		if(blogsByUser.containsKey(userId))
			blogsByUser.get(userId).deletePicture(blogId, postId, pictureId);

	}


	/**
	 * Returns the number of sounds from post
	 * @param blogId, postId
	 * 			- Identifier of the post sounds to be counted
	 * @return number of sounds
	 * @throws Exception
	 */
	public int getNumberOfSoundsOfPost(String userId, String blogId, String postId) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getNumberOfSoundsOfPost(blogId, postId);
		return 0;
		}

	/**
	 * Returns the number of pictures from post
	 * @param blogId, postId
	 * 			- Identifier of the post pictures to be counted
	 * @return number of pictures
	 * @throws Exception
	 */
	public int getNumberOfPicturesOfPost(String userId, String blogId, String postId) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getNumberOfPicturesOfPost(blogId, postId);
		return 0;	
		}
	
	/**
	 * Returns the number of movies from post
	 * @param blogId, postId
	 * 			- Identifier of the post movies to be counted
	 * @return number of movies
	 * @throws Exception
	 */
	public long getNumberOfMoviesOfPost(String userId, String blogId, String postId) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getNumberOfPicturesOfPost(blogId, postId);
		return 0;			
	}
	
	/**
	 * Capturing the sound of the post with the index passed as parameter
	 * @param blogId, postId
	 * 			- Identifier of the post which will capture the sound
	 * @param index
	 * 			- Sound's index
	 * @return soundId
	 * @throws Exception
	 */
	public String getSound(String userId, String blogId, String postId, int index) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getSound(blogId, postId, index);
		return "";				
	}
	
	/**
	 * Capturing the movie of the post with the index passed as parameter
	 * @param blogId, postId
	 * 			- Identifier of the post which will capture the movie
	 * @param index
	 * 			- Movie's index
	 * @return movieId
	 * @throws Exception
	 */
	public String getMovie(String userId, String blogId, String postId, int index) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getMovie(blogId, postId, index);
		return "";				
	}
	
	/**
	 * Capturing the picture of the post with the index passed as parameter
	 * @param blogId, postId
	 * 			- Identifier of the post which will capture the picture
	 * @param index
	 * 			- Picture's index
	 * @return pictureId
	 * @throws Exception
	 */
	public String getPicture(String userId, String blogId, String postId, int index) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getMovie(blogId, postId, index);
		return "";		
	}

	
	/**
	 * Returns data of sound
	 * @param soundId
	 * @return data of sound
	 * @throws Exception 
	 */
	public String getSoundData(String userId, String blogId, String postId, String mediaId) throws Exception{
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getSoundData(blogId, postId, mediaId);
		return "";	
	}
	
	/**
	 * Returns description of sound
	 * @param soundId
	 * @return description of sound
	 * @throws Exception 
	 */
	public String getSoundDescription(String userId, String blogId, String postId, String mediaId) throws Exception{
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getSoundDescription(blogId, postId, mediaId);
		return "";	
	}
	
	/**
	 * Returns data of picture
	 * @param picture Id
	 * @return data of picture
	 * @throws Exception 
	 */
	public String getPictureData(String userId, String blogId, String postId, String mediaId) throws Exception{
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getPictureData(blogId, postId, mediaId);
		return "";	

	}
	
	/**
	 * Returns description of picture
	 * @param pictureId
	 * @return description of picture
	 * @throws Exception 
	 */
	public String getPictureDescription(String userId, String blogId, String postId, String mediaId) throws Exception{
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getPictureDescription(blogId, postId, mediaId);
		return "";	

	}
	
	/**
	 * Returns data of movie
	 * @param movie Id
	 * @return data of movie
	 * @throws Exception 
	 */
	public String getMovieData(String userId, String blogId, String postId, String mediaId) throws Exception{
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getMovieData(blogId, postId, mediaId);
		return "";		
	}
	
	/**
	 * Returns description of movie
	 * @param movieId
	 * @return description of movie
	 * @throws Exception 
	 */
	public String getMovieDescription(String userId, String blogId, String postId, String mediaId) throws Exception{
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getMovieDescription(blogId, postId, mediaId);
		return "";	}

	//Comment Operations
	/**
	 * Add a comment
	 * @param blogId, postId
	 * 			- identifier of the post which is inserted into the comment
	 * @param comment
	 * 			- Text's comment
	 * @return
	 * @throws Exception
	 */
	public String addComment(String userId, String blogId, String postId, String comment, String login) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).addComment(blogId, postId, comment, login);
		return "";		
		
	}
	


	/**
	 * Returns the number of comments of post
	 * @param blogId, postId
	 * 			- Identifier of the post which will capture the number of comments
	 * @return number of posts
	 * @throws Exception
	 */
	public int getNumberOfComments(String userId, String blogId, String postId) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getNumberOfComments(blogId, postId);
		return 0; 
	}

	
	/**
	 * Capturing the comment with the index passed as parameter
	 * @param blogId, postId
	 * 			- Identifier of the post which will capture the comment
	 * @param index
	 * 			- comment's index
	 * @return commentId
	 */
	public String getComment(String userId, String blogId, String postId, int index) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getComment(blogId, postId, index);
		return "";
	}
	
//TODO JavaDoc
	public int getNumberOfCommentsByLogin(String userId, String blogId,
			String login) {
		
		return blogsByUser.get(userId).getNumberOfCommentsByLogin(blogId, login);
	}


	
	/**
	 * Retrieves the comment's text passed as parameter
	 * @param commentId
	 * 			- identifier of the comment that the text will be recovered
	 * @return comment's text
	 * @throws Exception
	 */
	public String getCommentText(String userId, String blogId, String postId, String commentId) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getCommentText(blogId, postId, commentId);
		return "";
		
	}

	/**
	 * Retrieves the comment's author passed as parameter
	 * @param commentId
	 * 			- identifier of the comment that the author will be recovered
	 * @return comment's author
	 * @throws Exception
	 */
	public String getCommentAuthor(String userId, String blogId, String postId, String commentId) throws Exception {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getCommentAuthor(blogId, postId, commentId);
		return "";		
	}

	public String addSubComment(String userId, String blogId, String postId, String commentId, String text) throws Exception{
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).addSubComment(blogId, postId, commentId, text, users.get(userId).getLogin());
		return "";
	}

	public String getSubComment(String userId, String blogId, String postId, String commentId, int index) throws Exception {
		if(blogsByUser.containsKey(blogId))
			return blogsByUser.get(blogId).getSubComment(blogId, postId, commentId, index);
		return "";
	}

	public int getNumberOfSubComments(String userId, String blogId, String postId, String commentId) throws Exception{
		if(blogsByUser.containsKey(blogId))
			return blogsByUser.get(blogId).getNumberOfSubComments(blogId, postId, commentId);
		return 0;		
	}

	public int getNumberOfAllSubComments(String userId, String blogId, String postId, String commentId) {
		if(blogsByUser.containsKey(userId))
			return blogsByUser.get(userId).getNumberOfAllSubComments(blogId, postId, commentId);
		return 0;		
		
	}
	
	//Search Operations
	/**
	 * Returns a list of profile by name
	 * @param match
	 * @return list of profile
	 * @throws Exception
	 */
	public List<String> findProfileByName(String match) throws Exception{
		List<String> usersForSearch = new ArrayList<String>();
		UserBean user;
		for (Iterator<UserBean> it = users.values().iterator(); it.hasNext();) {
			user = it.next();
			if(getInformationProfile(user.getLogin(), "Name").toUpperCase().contains(match.toUpperCase()))
				usersForSearch.add(user.getLogin());
		}
		return usersForSearch;
	}
	
	public List<String> findProfileByInterests(String match) throws Exception {
		
		List<String> usersForSearch = new ArrayList<String>();
		UserBean user;
		for (Iterator<UserBean> it = users.values().iterator(); it.hasNext();) {
			user = it.next();
			if(getInformationProfile(user.getLogin(), "Interests").toUpperCase().contains(match.toUpperCase()))
				usersForSearch.add(user.getLogin());
		}
		return usersForSearch;
	}
	
	/**
	 * Returns a list of profile by gender
	 * @param match
	 * @return list
	 * @throws Exception
	 */
	public List<String> findProfileByGender(String match) throws Exception {
		List<String> usersForSearch = new ArrayList<String>();

		if(match.equals("Não informado")){
			for (Iterator<UserBean> it = users.values().iterator(); it.hasNext();) 
				usersForSearch.add(it.next().getLogin());
		} else {

			UserBean user;
			for (Iterator<UserBean> it = users.values().iterator(); it.hasNext();) {
				user = it.next();
				if(getInformationProfile(user.getLogin(), "Gender").toUpperCase().equals(match.toUpperCase()))
					usersForSearch.add(user.getLogin());

			}
		}
		return usersForSearch;
	}
	
	/**
	 * Returns a list of blog by name passed as parameter
	 * @param match
	 * @return list
	 * @throws Exception
	 */
	public List<String> findBlogByName(String match) throws Exception {
		List<String> blogs = new ArrayList<String>();

		for (Iterator<BlogManager> it = blogsByUser.values().iterator(); it.hasNext();) 
			blogs.addAll(it.next().findBlogByName(match));
				
		return blogs;
	}

	
	
	private boolean existEmail(String email){
		for(Iterator<UserBean> it = users.values().iterator(); it.hasNext();)
			if(it.next().getEmail().equals(email))
				return true;
		return false;
	}
	
	/**
	 * Converts a string to a valid Date.
	 * 
	 * @param date - String to be converted
	 * @return A Calendar instance if could parte into a Date, null otherwise.
	 */
	private Calendar parseStringToDate(String date) {
		try {
			Calendar cal = new GregorianCalendar();

			String[] arrayStringdate = date.split("/");
			String day = arrayStringdate[0];
			String month = arrayStringdate[1];
			String year = arrayStringdate[2];

			cal.setLenient(false);
			cal.set(GregorianCalendar.YEAR, new Integer(year));
			cal.set(GregorianCalendar.MONTH, new Integer(month) - 1); // Jan = 0, Fev = 1...
			cal.set(GregorianCalendar.DATE, new Integer(day));

			cal.getTime(); // exception thrown here if date is invalid

			return cal;

		} catch (Exception e) {
			return null;
		}
	}
	

	public void save(){
		DBFacade.getInstance().saveUsers(users.values());
	}


	
	private String getNewUserId() {
		return String.valueOf(DBFacade.getInstance().getLastUserId());
	}









}
